<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import useUserStore from '@/stores/modules/user'
const userStore = useUserStore()
const router = useRouter()

const userForm = ref({
  account: import.meta.env.VITE_APP_ACCOUNT,
  password: import.meta.env.VITE_APP_PASSWORD,
  checked: import.meta.env.VITE_APP_CHECKED
})
const formRef = ref(null)
const rule = ref({
  account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})
const onSubmit = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      if (userForm.value.checked) {
        // console.log(userForm.value)
        const login = await userStore.login(userForm.value)
        if (login.code === 200) {
          ElMessage({
            message: '登陆成功',
            type: 'success'
          })
          router.push('/')
        } else {
          ElMessage.error(login.msg)
        }
      } else {
        // console.log('注册')
        show.value = true
      }
    } else {
      return false
    }
  })
}

const show = ref(false)
const handleClose = () => {
  show.value = false
}
const agree = () => {
  // console.log('同意')
  userForm.value.checked = true
  show.value = false
  onSubmit()
}
</script>

<template>
  <div class="login">
    <div class="login_cont">
      <div class="form">
        <h1>账号登录</h1>
        <div>
          <el-form :model="userForm" ref="formRef" :rules="rule">
            <el-form-item prop="account">
              <el-input size="large" v-model="userForm.account" placeholder="请输入账号" />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                size="large"
                v-model="userForm.password"
                placeholder="请输入密码"
                show-password
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="privacy">
          <div class="checkbox disCenter">
            <el-checkbox v-model="userForm.checked" />
            <span class="ml10" style="margin-left: 10px">
              <span>我已阅读并同意</span>
              <span class="link">《用户协议》</span>
              <span>和</span>
              <span class="link">《隐私政策》</span>
            </span>
          </div>
        </div>
        <div class="btn">
          <button class="btnLog" @click="onSubmit">登录</button>
        </div>
        <div class="forget footB fz14">
          <a>忘记密码</a>
        </div>
        <div class="register footB fz14">
          没有账号？ <router-link class="link linka" to="/register">立即注册</router-link>
        </div>
      </div>
    </div>
    <el-dialog v-model="show" width="400" align-center :before-close="handleClose">
      <template #header="{ titleId, titleClass }">
        <div class="my-header">
          <div :id="titleId" :class="titleClass">用户协议及隐私保护</div>
        </div>
      </template>
      <div class="content">
        请您认真阅读<span class="link">《用户协议》</span>和<span class="link">《隐私政策》</span
        >的全部条款，同意后可以开始使用我们的服务。
      </div>
      <template #footer>
        <div class="dialog-footer">
          <button class="btnLogNull" @click="handleClose">不同意</button>
          <button class="btnLog" @click="agree">同意并登录</button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.login {
  width: 100vw;
  height: 100vh;
  background: #fff;
  background-image: url('@/assets/images/login.png');
  background-size: cover;
  background-position: center;
  .login_cont {
    position: absolute;
    top: 50%;
    right: 440px;
    padding-bottom: 30px;
    width: 464px;
    background: #fff;
    box-shadow:
      0 12px 40px 16px rgba(8, 25, 93, 0.03),
      0 8px 20px 0 rgba(8, 25, 93, 0.06),
      0 4px 8px 0 rgba(8, 25, 93, 0.08);
    transform: translateY(-50%);
    border-radius: 8px;
    .form {
      padding: 0 48px;
      width: 464px;
      background: #fff;
      border-radius: 8px;
      overflow: hidden;
      box-sizing: border-box;
      h1 {
        margin: 48px auto 24px 136px;
        height: 32px;
        font-size: 24px;
        font-family:
          PingFangSC-Semibold,
          PingFang SC;
        font-weight: 600;
        color: #1d2129;
        line-height: 32px;
      }
      .el-form-item {
        margin-bottom: 25px;
      }
      .link {
        color: #165dff;
        cursor: pointer;
      }
      .privacy {
        font-size: 14px;
      }
      .btn {
        margin-top: 25px;
        .btnLog {
          width: 100%;
          height: 48px;
          border: none;
          border-radius: 5px;
          color: #fff;
          font-size: 16px;
          background: #165dff;
          outline: none;
          cursor: pointer;
        }
        .btnLog:hover {
          background: #4080ff;
        }
      }
      .forget {
        margin-top: 15px;
        a {
          cursor: pointer;
        }
        a:hover {
          color: #165dff;
        }
      }
      .footB {
        line-height: 35px;
        text-align: center;
        .linka {
          color: #165dff;
          text-decoration: none;
        }
      }
    }
  }
  .content {
    padding: 20px;
    color: #4e5969;
    .link {
      color: #165dff;
      cursor: pointer;
    }
  }
  .btnLogNull {
    width: 82px;
    height: 32px;
    border: none;
    border-radius: 5px;
    outline: none;
    cursor: pointer;
    font-size: 14px;
    background: #f2f3f5;
    color: #000;
  }
  .btnLogNull:hover {
    background: #e5e6eb;
  }
  .btnLog {
    margin-left: 15px;
    width: 110px;
    height: 32px;
    border: none;
    border-radius: 5px;
    color: #fff;
    background: #165dff;
    outline: none;
    cursor: pointer;
    font-size: 14px;
  }
  .btnLog:hover {
    background: #4080ff;
  }
}
</style>
